// pages/goods/goods.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navHeight: 0,
        menuButtonInfo: {
            bottom: 0,
            height: 0,
            top: 0,
            left: 0,
            right: 0,
            width: 0,
        },
        //商品
        good: {},
        dis_list: [{}],
        url: '',
        open_index: -1,
        //商品id
        id: -1,
        //触底标志
        again: false,
        page: 1,
        //评论详情
        chose:1

    },
    change(event: any) {
        console.log(event.target.dataset.chose)
        this.setData({
            chose: event.target.dataset.chose
        })
    },
    //写评论
    write() {
        wx.navigateTo({
            url: `/pages/discussion/discussion?id=${this.data.good.id}&img=${this.data.good.picture}`

        })
    },
    //返回
    back() {
        wx.navigateBack({
            delta: 1
        })
    },
    //预览图片
    openimage(event: any) {
        let now=event.currentTarget.dataset.all;
           now=now.split(',');
           if(now.length===1){
            let url=[];
             url.push(`${this.data.url}${now}`)
            wx.previewImage({
                current:url[0],// 当前显示图片的http链接
                urls:url// 需要预览的图片http链接列表
            })
           }else{
            let all = JSON.parse(event.currentTarget.dataset.all);
            const index = event.target.dataset.index;
            console.log(all)
            for (var i = 0; i < all.length; i++) {
                all[i] = this.data.url + all[i];
            }
            wx.previewImage({
                current: all[index],// 当前显示图片的http链接
                urls: all// 需要预览的图片http链接列表
            })
           }
    
        },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option: { id: number }) {
        wx.showLoading({
            title: '小的正在努力！',
          })
        var app = getApp();
        const that = this;
        //广告
        this.setData({
            url: app.globalData.url,
            id: option.id,
        })
        console.log(option.id)
        //文章
        wx.request({
            url: `${app.globalData.url}api/product?id=${option.id}`, //仅为示例，并非真实的接口地址
            success(res: { data: { code: 0, data: { data: { id: number, news_image: string, news_name: string }[] }[] } }) {
                console.log(res);
                const info = res.data.data;
                console.log(info)
                // that.data.background=info
                that.setData({
                    good: info[0]
                })
                //评论
        that.article();
            }
        })
        
        this.setData({
            //获取胶囊一块的宽高
            menuButtonInfo: wx.getMenuButtonBoundingClientRect()
        })
        // console.log(this.data.menuButtonInfo);
        //当前小程序运行在企业微信环境中,返回设备信息
        wx.getSystemInfo({
            success: (res) => {
                const { statusBarHeight } = res
                //结构状态栏的高度，单位px 减去上面的得边距
                console.log(statusBarHeight)
                const margin = this.data.menuButtonInfo.top - statusBarHeight
                console.log(margin)
                this.setData({
                    //总上边距
                    navHeight: (this.data.menuButtonInfo.height + statusBarHeight + (margin * 2)),
                    searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
                    searchHeight: this.data.menuButtonInfo.height,  // 与胶囊按钮同高
                    searchWidth: this.data.menuButtonInfo.right - this.data.menuButtonInfo.width, // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
                    //   main:214
                })
            },
        })
    },
    //打开详情
    open(event: any) {
        console.log(event.target.dataset.index)
        this.setData({
            open_index: event.target.dataset.index
        })
    },
    again() {
        console.log(55555555555)
    },
    article() {
        //文章评价
        var app = getApp();
        const that = this;
        console.log(this.data.good.name)
        wx.request({
            url: `${app.globalData.url}api/comment?id=${this.data.id}&page=${this.data.page}`, //仅为示例，并非真实的接口地址
            success(res: { data: { code: number, data: { data: { id: number, news_image: string, news_name: string }[] }[] } }) {
                console.log(res);
                const info = res.data.data;
                console.log(info)
                console.log(res.data.code)
                //关掉提示加载
                wx.hideLoading();
                if(info){
                    if (info.length < 5) {
                        that.setData({
                            again: true,
                        })
                    }
                    if (that.data.page === 1) {
                        console.log(1111)
                        that.setData({
                            dis_list: info
                        })
                    } else {
                        that.setData({
                            dis_list: that.data.dis_list.concat(info)
                        })
                    }
                }else{
                    console.log(222222)
                    that.setData({
                        dis_list:[],
                        again: true,
                    })
                }
                
                

            }

        })
        

},

/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady() {

},

/**
 * 生命周期函数--监听页面显示
 */
onShow() {
    this.article();
},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide() {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload() {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh() {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom() {
    if(!this.data.again){
        this.setData({
            page:this.data.page+1
        })
        this.article();
    }
    
},

/**
 * 用户点击右上角分享
 */
onShareAppMessage() {

}
})